//@author A0084176Y
package Main;

import java.util.*;

public class ManageCategory {
	
	protected Vector<String> e_cat_list;
	protected Vector<String> p_cat_list;
	protected Vector<String> temp;
	protected Stack<Integer> estack = new Stack<Integer>();
	protected Stack<Integer> pstack = new Stack<Integer>();
	protected int noOfExpDelete = 0;
	protected Stack<Integer> expIndicator = new Stack<Integer>();
	protected int noOfPayDelete = 0;
	protected Stack<Integer> payIndicator = new Stack<Integer>();
	
	public ManageCategory(){
		e_cat_list=new Vector<String>();	
		e_cat_list.add("Food");
		e_cat_list.add("Transport");
		e_cat_list.add("Entertainment");
		e_cat_list.add("Stationery");
		e_cat_list.add("Miscellaneous");
		
		p_cat_list=new Vector<String>();
		p_cat_list.add("Cash");
		p_cat_list.add("Credit");
		p_cat_list.add("Miscellaneous");
	}
	
	public Vector<String> getECat(){
		return e_cat_list;
	}
	public Vector<String> getPCat(){
		return p_cat_list;
	}

	public int create(String name, String type){
		int check = 0;
		
		if(type.equals("expense")){
			temp=e_cat_list;
			for(int i = 0; i < temp.size(); i++){
				if (temp.get(i).equals(name)){
					check = 1;
				}
			}
		}
		else{
			temp=p_cat_list;
			for(int i = 0; i < temp.size(); i++){
				if (temp.get(i).equals(name)){
					check = 1;
				}
			}
		}
		if(check == 0){
			temp.add(name);
			return check;
		}
		else{
			return check;
		}
		
	}
	
	public void change(ManageItem obj, String name, String type){
		if(type.equals("expense")){
			while(!expIndicator.isEmpty() && expIndicator.peek() == noOfExpDelete){
				expIndicator.pop();
				int i = estack.pop();
				obj.getList().get(i).setCategory(name);
			}
			noOfExpDelete --;
		}
		else{
			while(!payIndicator.isEmpty() && payIndicator.peek() == noOfPayDelete){
				payIndicator.pop();
				int i = pstack.pop();
				obj.getList().get(i).setPay(name);
			}
			noOfPayDelete --;
		}
	}
	
	public int delete(String name, ManageItem obj,String type){
		if(type.equals("expense"))
			temp=e_cat_list;
		else
			temp=p_cat_list;
		
		int delete=-1;
		for(int i=0;i<temp.size();i++){
			if (temp.get(i).equals(name)){
				delete=i;
				if(type.equals("expense")){
					noOfExpDelete ++;
				}
				else{
					noOfPayDelete ++;
				}
			}
		}
		if(delete != -1){
			for(int i=0;i<obj.getList().size();i++){				//editing the itemlist
				if(type.equals("expense")){
					if(obj.getList().get(i).getCategory().equals(name)){
						obj.getList().get(i).setCategory("Miscellaneous");
						estack.push(i);
						expIndicator.push(noOfExpDelete);
					}
				}	
				else{
					if(obj.getList().get(i).getPay().equals(name)){
						obj.getList().get(i).setPay("Miscellaneous");
						pstack.push(i);
						payIndicator.push(noOfPayDelete);
					}
				}
			}
		}
		
		if(delete!=-1){
			temp.remove(delete);
			return delete;
		}
		else
			return delete;
	}
	
	public int update(String name_old, String name_new, ManageItem obj, String type){
		if(type.equals("expense"))
			temp=e_cat_list;
		else
			temp=p_cat_list;
		
		int old=-1;
		for(int i=0;i<temp.size();i++){			//updating the category in the cat vector
			if(temp.get(i).equals(name_old)){
				old=i;
				temp.remove(old);
				temp.add(old, name_new);
			}
		}
		if(old!=-1){
		for(int i=0;i<obj.getList().size();i++){		//updating the category in the itemlist
			if(type.equals("expense")){
				if(obj.getList().get(i).getCategory().equals(name_old)){
					obj.getList().get(i).setCategory(name_new);
				}
			}
			else{
				if(obj.getList().get(i).getPay().equals(name_old)){
					obj.getList().get(i).setPay(name_new);
				}
			}
		}
		return old;
		}
		else
			return old;
	}
	
	public Vector<Category> print(ManageItem obj, String type){
		if(type.equals("expense"))
			temp=e_cat_list;
		else
			temp=p_cat_list;
		
		Vector<Category>vec=new Vector<Category>();
		for(int i=0;i<temp.size();i++){
			Category c=new Category(temp.get(i));
			for(int j=0;j<obj.getList().size();j++){
				if(type.equals("expense")){
				if(obj.getList().get(j).getCategory().equals(temp.get(i))){
					c.addItem(obj.getList().get(j));
				}
				}
				else{
					if(obj.getList().get(j).getPay().equals(temp.get(i))){
						c.addItem(obj.getList().get(j));
					}
				}
			}
			vec.add(c);
		}
		return vec;
	}

}